from typing import Annotated, Optional

from fastapi import Query
from pydantic import model_validator

from app.utils import types
from app.models.common import AppId, Pagination
from app.models import responses


class AppDeviceLogList(AppId, Pagination):
    """分页获取指定应用的设备日志记录"""
    device_code: str
    device_ip: str
    event_type: Optional[int]


    @model_validator(mode='before')
    @classmethod
    def before(cls, data):
        data = types.safe_dict(data)
        data['device_code'] = data.get('device_code', '')
        data['device_ip'] = data.get('device_ip', '')
        data['event_type'] = types.safe_int(data.get('event_type', None), None)
        return data

    @model_validator(mode='after')
    def after(self):
        if len(self.device_code) > 128:
            raise responses.FailureResponseError(-102, msg='设备码长度不能超过 128 位')

        if len(self.device_ip) > 64:
            raise responses.FailureResponseError(-102, msg='设备码 IP 长度不能超过 64 位')

        if self.event_type is not None and not (0 <= self.event_type <= 255):
            raise responses.FailureResponseError(-102, msg='事件类型的状态值介于 0-255')

        return self


AppDeviceLogListParams = Annotated[AppDeviceLogList, Query()]
